package com.york.tinyrpc.protoss.enums;

import cn.hutool.core.util.ObjectUtil;
import lombok.Getter;

/**
 * 协议的序列化器枚举
 */
@Getter
public enum TinyRpcProtocolMessageSerializationEnum {


    /**
     * JDK序列化
     */
    JDK_SERIALIZER("jdk", 0),

    /**
     * Json序列化
     */
    JSON_SERIALIZER("json", 1),

    /**
     * Kryo序列化
     */
    KRYO_SERIALIZER("kryo", 2),

    /**
     * Hessian序列化
     */
    HESSIAN_SERIALIZER("hessian", 3);


    /**
     * 序列化器名称
     */
    private final String serializerName;

    /**
     * 序列化器value
     */
    private final int value;

    TinyRpcProtocolMessageSerializationEnum(String serializerName, int value) {
        this.serializerName = serializerName;
        this.value = value;
    }

    /**
     * 根据序列化器value获取枚举
     *
     * @param value
     * @return
     */
    public static TinyRpcProtocolMessageSerializationEnum getEnumByValue(int value) {
        for (TinyRpcProtocolMessageSerializationEnum anEnum : TinyRpcProtocolMessageSerializationEnum.values()) {
            if (anEnum.value == value) {
                return anEnum;
            }
        }
        return null;
    }

    /**
     * 根据序列化器名称获取枚举
     *
     * @param serializerName
     * @return
     */
    public static TinyRpcProtocolMessageSerializationEnum getEnumByName(String serializerName) {
        if (ObjectUtil.isEmpty(serializerName)) {
            return null;
        }

        for (TinyRpcProtocolMessageSerializationEnum anEnum : TinyRpcProtocolMessageSerializationEnum.values()) {
            if (anEnum.serializerName.equals(serializerName)) {
                return anEnum;
            }
        }
        return null;
    }

}
